package cn.car.service.impl;

import cn.car.domains.entity.VehicleEntryExitLedgerInfoEntity;
import cn.car.domains.enums.InoutTypeEnum;
import cn.car.domains.enums.VehicleSnapshotTypeEnum;
import cn.car.mapper.VehicleEntryExitLedgerInfoMapper;
import cn.car.service.VehicleEntryExitLedgerInfoService;
import cn.hutool.core.date.DateTime;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleEntryExitLedgerInfoServiceImpl implements VehicleEntryExitLedgerInfoService {

    @Resource
    private VehicleEntryExitLedgerInfoMapper vehicleEntryExitLedgerInfoMapper;

    @Override
    public void insert(VehicleEntryExitLedgerInfoEntity vehicleEntryExitLedgerInfoEntity) {
        vehicleEntryExitLedgerInfoMapper.insert(vehicleEntryExitLedgerInfoEntity);
    }

    @Override
    public VehicleEntryExitLedgerInfoEntity getLastLedgerByVehicleNumber(String vehicleNumber) {
        return vehicleEntryExitLedgerInfoMapper.getLastLedgerByVehicleNumber(vehicleNumber);
    }

    @Override
    public void recordHeadTailImg(Long id, String filePath, String inOutType, VehicleSnapshotTypeEnum vehicleSnapshotTypeEnum) {

        VehicleEntryExitLedgerInfoEntity updateEntity = new VehicleEntryExitLedgerInfoEntity();
        updateEntity.setId(id);

        if (InoutTypeEnum.in.name().equals(inOutType)) {
            setEntryImage(updateEntity, filePath, vehicleSnapshotTypeEnum);
        } else {
            setExitImage(updateEntity, filePath, vehicleSnapshotTypeEnum);
        }

        vehicleEntryExitLedgerInfoMapper.updateById(updateEntity);
    }

    @Override
    public void update(VehicleEntryExitLedgerInfoEntity lastVehicleEntity) {
        vehicleEntryExitLedgerInfoMapper.updateById(lastVehicleEntity);
    }

    @Override
    public List<VehicleEntryExitLedgerInfoEntity> compensateVehicleLedgerInfoUploadStatus(DateTime startTime, DateTime endTime) {
        return vehicleEntryExitLedgerInfoMapper.compensateVehicleLedgerInfoUploadStatus(startTime, endTime);
    }

    private void setExitImage(VehicleEntryExitLedgerInfoEntity updateEntity, String filePath, VehicleSnapshotTypeEnum vehicleSnapshotTypeEnum) {
        switch (vehicleSnapshotTypeEnum) {
            case HEAD -> updateEntity.setExitHeadImgUrl(filePath);
            case MID -> updateEntity.setExitMidImgUrl(filePath);
            case TAIL -> updateEntity.setExitTailImgUrl(filePath);
            default -> throw new IllegalArgumentException("Unknown snapshot type: " + vehicleSnapshotTypeEnum.name());
        }
    }

    private void setEntryImage(VehicleEntryExitLedgerInfoEntity updateEntity, String filePath, VehicleSnapshotTypeEnum vehicleSnapshotTypeEnum) {
        switch (vehicleSnapshotTypeEnum) {
            case HEAD -> updateEntity.setEntryHeadImgUrl(filePath);
            case MID -> updateEntity.setEntryMidImgUrl(filePath);
            case TAIL -> updateEntity.setEntryTailImgUrl(filePath);
            default -> throw new IllegalArgumentException("Unknown snapshot type: " + vehicleSnapshotTypeEnum.name());
        }
    }
}
